package com.example.tys.daggermvp.mvp.view;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.tys.daggermvp.BaseActivity;
import com.example.tys.daggermvp.R;
import com.example.tys.daggermvp.bean.Test;
import com.example.tys.daggermvp.mvp.contract.MainActivityContract;
import com.example.tys.daggermvp.network.retrofit2.RetrofitDefault;
import com.example.tys.daggermvp.utils.DisplayUtil;

import java.io.IOException;

import javax.inject.Inject;

import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.Result;
import retrofit2.http.GET;


/**
 * @author tys
 */
public class MainActivity extends BaseActivity<MainActivityContract.Presenter, MainActivityContract.View, MainActivityContract.Date>
        implements MainActivityContract.View {

    @Inject
    MainActivityContract.Presenter presenter;
    @Inject
    MainActivityContract.Date date;
    private String currentThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e("1111", String.valueOf(DisplayUtil.dp2px(1)));
    }


    public void bt(View view) {
        mPresenter.say("tys");
    }

    @Override
    public void say(String v) {
        Toast.makeText(this, "0-0-" + v, Toast.LENGTH_SHORT).show();
        startActivity(new Intent(this, Main2Activity.class));
    }

    public void testRe(View view) {
        Retrofit retrofit = RetrofitDefault.getRetrofit();
        GitHubService service = retrofit.create(GitHubService.class);

        //Retrofit
//        Call<ResponseBody> call = service.listRepos();
//        call.enqueue(new Callback<ResponseBody>() {
//            @Override
//            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//                Log.e("---", "onResponse:" + response.toString() + getCurrentThread());
//            }
//
//            @Override
//            public void onFailure(Call<ResponseBody> call, Throwable t) {
//                Log.e("---", "onFailure:" + t.toString() + getCurrentThread());
//            }
//        });

        //Rxjava BodyObservable
        Observable<Test> call = service.listRepos();
        call.subscribe(new Observer<Test>() {
            @Override
            public void onSubscribe(Disposable d) {//Main 线程
                Log.e("---", "onSubscribe:" + d.toString() + getCurrentThread());
            }

            @Override
            public void onNext(Test value) {//子线程
                Log.e("---", "onNext:" + value.toString() + getCurrentThread());
            }

            @Override
            public void onError(Throwable e) {//子线程
                Log.e("---", "onError:" + e.toString() + getCurrentThread());
            }

            @Override
            public void onComplete() {//子线程
                Log.e("---", "onComplete:" + getCurrentThread());
            }
        });


    }

    public String getCurrentThread() {
        return " " + Thread.currentThread().getName() + " - " + Thread.currentThread().getId();
    }

    public interface GitHubService {
        //api/qqmusic/8446666
        @GET("/api/qqmusic/925901009")
//        Call<ResponseBody> listRepos(); //默认CallAapter处理,直接调用的okhttp.Call 的execute 或者enqueue方法
        Observable<Test> listRepos();//
    }

}



